<?php namespace App\Http\Controllers\Api\Admin;

use App\AboutUs;
use App\Admin;
use App\ApplicationField;
use App\Banner;
use App\BooksChapter;
use App\BooksInfo;
use App\Classify;
use App\EnAboutUs;
use App\EnApplicationField;
use App\EnBanner;
use App\EnFooterMenu;
use App\EnGoodsProduct;
use App\EnHumanResources;
use App\EnNews;
use App\EnSysInfo;
use App\FooterMenu;
use App\GoodsProduct;
use App\Http\Controllers\Controller;
use App\HumanResources;
use App\RechargePackage;
use App\SysInfo;
use App\User;
use App\VipRule;
use Auth;
use EasyWeChat\Kernel\Messages\News;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;

class SystemController extends Controller
{
    public function setBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title   = $request->get('title');
        $linkUrl = $request->get('link_url');
        $sort    = $request->get('sort');
        $imgId   = $request->get('img_id');
        $type    = !is_null($request->get('type')) ? $request->get('type') : '';
        $id      = !is_null($request->get('id'))?$request->get('id'):0;

        if($type == 'banner'){
            $thatALLBanner = Banner::whereId(trim($id))->first();

            if(!$thatALLBanner)
                $thatALLBanner = new Banner();

            if(!is_null($title))$thatALLBanner->title = $title;
            if(!is_null($linkUrl))$thatALLBanner->link_url = $linkUrl;
            if(!is_null($sort))$thatALLBanner->sort = $sort;
            if(!is_null($imgId))$thatALLBanner->img_id = $imgId;
            $thatALLBanner->save();
        }elseif($type == 'en_banner'){
            $thatALLBanner = EnBanner::whereId(trim($id))->first();

            if(!$thatALLBanner)
                $thatALLBanner = new EnBanner();

            if(!is_null($title))$thatALLBanner->title = $title;
            if(!is_null($linkUrl))$thatALLBanner->link_url = $linkUrl;
            if(!is_null($sort))$thatALLBanner->sort = $sort;
            if(!is_null($imgId))$thatALLBanner->img_id = $imgId;
            $thatALLBanner->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$thatALLBanner]);
    }

    public function delBanner(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type    = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'banner'){
            /** @var Banner $thatBanner */
            $thatBanner = Banner::whereId($id)->first();
        }elseif($type == 'en_banner'){
            /** @var EnBanner $thatBanner */
            $thatBanner = EnBanner::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatBanner)
            return response()->json(["level"=>"error","message"=>"该banner不存在！"]);

        $thatBanner->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatBanner]);
    }

    public function setAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $username = $request->get('username');
        $password = $request->get('password');
        $repass   = $request->get('repass');

        if($password != $repass)
            return response()->json(['level'=>'error','message' => '两次密码不一致！']);

        /** @var Admin $hasUser */
        $hasUser = Admin::whereUsername($username)->first();
        if($hasUser){
            return response()->json(['level'=>'error','message' => '当前管理员账号已存在']);
        }

        $thatAdmin = new Admin();
        $thatAdmin->username = $username;
        $thatAdmin->password = bcrypt($password);
        $thatAdmin->status = 2;
        $thatAdmin->save();

        return response()->json(['level'=>'success','message'=>'添加成功','data'=>$thatAdmin]);
    }

    public function delAdmin(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;

        /** @var Admin $thatAdmin */
        $thatAdmin = Admin::whereId($id)->first();

        if(!$thatAdmin)
            return response()->json(["level"=>"error","message"=>"该管理员不存在！"]);

        if($thatAdmin->status == 1)
            return response()->json(["level"=>"error","message"=>"该管理员不能删除！"]);


        $thatAdmin->forceDelete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatAdmin]);
    }

    public function addClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $sort = $request->get('sort');
        $id   = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId(trim($id))->first();

        if(!$newClassify)
            $newClassify = new Classify();

        if(!is_null($name))$newClassify->name = $name;
        if(!is_null($sort))$newClassify->sort = $sort;
        $newClassify->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

    public function setWebSiteInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $title       = $request->get('title');
        $url         = $request->get('url');
        $footerInfo  = $request->get('footer_info');
        $logoId      = !is_null($request->get('logo_id')) ? $request->get('logo_id') : 0;

        if(!is_null($title)){
            $thisTitle = SysInfo::whereType('title')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'title';
            }

            $thisTitle->content   = $title;
            $thisTitle->save();
        }

        if(!is_null($url)){
            $thisTitle = SysInfo::whereType('url')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'url';
            }

            $thisTitle->content   = $url;
            $thisTitle->save();
        }

        if(!is_null($footerInfo)){
            $thisTitle = SysInfo::whereType('footer')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'footer';
            }

            $thisTitle->content   = $footerInfo;
            $thisTitle->save();
        }

        if($logoId != 0){
            $thisTitle = SysInfo::whereType('logo')->first();
            if(!$thisTitle){
                $thisTitle = new SysInfo();
                $thisTitle->type  = 'logo';
            }

            $thisTitle->content   = $logoId;
            $thisTitle->save();
        }

        return response()->json(['level' => 'success','message' => '信息设置成功']);
    }

    public function addFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $pid  = !is_null($request->get('pid')) ? $request->get('pid') : 0;
        $name = $request->get('name');
        $link = $request->get('link');

        $isFooterMenu = FooterMenu::whereId($pid)->first();
        if(!$isFooterMenu)
            return response()->json(['level' => 'error','message' => '当前父级栏目不存在']);

        $newFooterMenu = new FooterMenu();
        $newFooterMenu->pid                     = $pid;
        if(!is_null($name))$newFooterMenu->name = $name;
        if(!is_null($link))$newFooterMenu->link = $link;
        $newFooterMenu->save();

        return response()->json(['level' => 'success','message' => '新增成功','data' => $newFooterMenu]);
    }

    public function delFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");

        /** @var FooterMenu $theMenu */
        $theMenu = FooterMenu::whereId($id)->first();
        if(!$theMenu)
            return response()->json(["level"=>"error","message"=>"当前栏目不存在"]);

        $theMenu->delete();

        return response()->json(["level"=>"success","message"=>"信息设置成功"]);

    }

    public function setGoods(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $name                      = $request->get('name');
        $desc                      = $request->get('desc');
        $imgId                     = $request->get('thumb_id');
        $content                   = $request->get('content');
        $isShop                    = $request->get('is_shop');
        $type                      = !is_null($request->get('type')) ? $request->get('type') : '';
        $sort                      = $request->get('sort');
        $classifyId                = $request->get('classify_id');
        $id                        = !is_null($request->get('id'))?$request->get('id'):0;

        if($type == 'product'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllGoods = GoodsProduct::whereId($id)->first();
            if(!$newAllGoods)
                $newAllGoods = new GoodsProduct();

            if(!is_null($name))$newAllGoods->name                      = $name;
            if(!is_null($desc))$newAllGoods->desc                      = $desc;
            if(!is_null($imgId))$newAllGoods->thumb_id                 = $imgId;
            if(!is_null($content))$newAllGoods->content                = $content;
            if(!is_null($isShop))$newAllGoods->is_shop                 = $isShop;
            if(!is_null($sort))$newAllGoods->sort                      = $sort;
            if(!is_null($classifyId))$newAllGoods->classify_id         = $classifyId;
            $newAllGoods->save();
        }elseif($type == 'en_product'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllGoods = EnGoodsProduct::whereId($id)->first();
            if(!$newAllGoods)
                $newAllGoods = new EnGoodsProduct();

            if(!is_null($name))$newAllGoods->name                      = $name;
            if(!is_null($desc))$newAllGoods->desc                      = $desc;
            if(!is_null($imgId))$newAllGoods->thumb_id                 = $imgId;
            if(!is_null($content))$newAllGoods->content                = $content;
            if(!is_null($isShop))$newAllGoods->is_shop                 = $isShop;
            if(!is_null($sort))$newAllGoods->sort                      = $sort;
            if(!is_null($classifyId))$newAllGoods->classify_id         = $classifyId;
            $newAllGoods->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(['level' => 'success','message' => '添加或修改成功',"data"=>$newAllGoods]);
    }

    public function delGoods(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'product'){
            $thatGoods = GoodsProduct::whereId($id)->first();
        }elseif($type == 'en_product'){
            $thatGoods = EnGoodsProduct::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误！']);
        }

        if(!$thatGoods)
            return response()->json(["level"=>"error","message"=>"该商品不存在！"]);

        $thatGoods->delete();

        return response()->json(["level"=>"success","message"=>"删除成功","data"=>$thatGoods]);
    }

    public function addAllClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $name = $request->get("name");
        $sort = $request->get('sort');
        $type = $request->get('type');
        $id   = !is_null($request->get('id'))?$request->get('id'):0;

        $newClassify = Classify::whereId(trim($id))->first();
        if(!$newClassify){
            $newClassify = new Classify();
            if(!is_null($type))$newClassify->type = $type;
        }


        if(!is_null($name))$newClassify->name = $name;
        if(!is_null($sort))$newClassify->sort = $sort;
        $newClassify->save();

        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newClassify]);
    }

    public function delAllClassify(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $thatClassify =Classify::whereId($id)->first();

        if(!$thatClassify)
            return response()->json(["level"=>"error","message"=>"该分类不存在！"]);

        $thatClassify->delete();

        return response()->json(['level' => 'success','message' => '删除成功']);
    }

    public function setNews(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $title        = $request->get("title");
        $desc         = $request->get('desc');
        $thumbImgId   = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $content      = $request->get('content');
        $isShow       = !is_null($request->get('is_show')) ? $request->get('is_show') : 0;
        $classifyId   = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        if($type == 'news'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllNews = \App\News::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new \App\News();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->is_show                                   = $isShow;
            $newAllNews->classify_id                               = $classifyId;
            $newAllNews->save();
        }elseif($type == 'en_news'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllNews = EnNews::whereId($id)->first();
            if(!$newAllNews)
                $newAllNews = new EnNews();

            if(!is_null($title))$newAllNews->title                 = $title;
            if(!is_null($desc))$newAllNews->desc                   = $desc;
            $newAllNews->thumb_img_id                              = $thumbImgId;
            if(!is_null($content))$newAllNews->content             = $content;
            $newAllNews->is_show                                   = $isShow;
            $newAllNews->classify_id                               = $classifyId;
            $newAllNews->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllNews]);
    }

    public function delNews(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'news'){
            $thatNews =\App\News::whereId($id)->first();
        }elseif($type == 'en_news'){
            $thatNews = EnNews::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误!']);
        }

        if(!$thatNews)
            return response()->json(["level"=>"error","message"=>"该新闻不存在！"]);

        $thatNews->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatNews]);
    }

    public function setAboutUs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $content      = $request->get('content');
        $isShow       = !is_null($request->get('is_show')) ? $request->get('is_show') : 0;
        $classifyId   = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        if($type == 'about_us'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllAboutUs = AboutUs::whereId($id)->first();

            $isAboutUs = AboutUs::whereClassifyId($classifyId)->first();

            if($newAllAboutUs && ($isAboutUs && $newAllAboutUs->id != $isAboutUs->id))
                return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);

            if(!$newAllAboutUs){
                $newAllAboutUs = new AboutUs();

                if($isAboutUs)
                    return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);
            }

            if(!is_null($content))$newAllAboutUs->content             = $content;
            $newAllAboutUs->is_show                                   = $isShow;
            $newAllAboutUs->classify_id                               = $classifyId;
            $newAllAboutUs->save();
        }elseif($type == 'en_about_us'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllAboutUs = EnAboutUs::whereId($id)->first();

            $isAboutUs = EnAboutUs::whereClassifyId($classifyId)->first();

            if($newAllAboutUs && ($isAboutUs && $newAllAboutUs->id != $isAboutUs->id))
                return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);

            if(!$newAllAboutUs){
                $newAllAboutUs = new EnAboutUs();

                if($isAboutUs)
                    return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);
            }

            if(!is_null($content))$newAllAboutUs->content             = $content;
            $newAllAboutUs->is_show                                   = $isShow;
            $newAllAboutUs->classify_id                               = $classifyId;
            $newAllAboutUs->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllAboutUs]);
    }

    public function delAboutUs(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id   = !is_null($request->get('id'))?$request->get('id'):0;
        $type = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'about_us'){
            $thatAboutUs = AboutUs::whereId($id)->first();
        }elseif($type == 'en_about_us'){
            $thatAboutUs = EnAboutUs::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatAboutUs)
            return response()->json(["level"=>"error","message"=>"该信息不存在！"]);

        $thatAboutUs->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatAboutUs]);
    }

    public function setHumanRes(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $content      = $request->get('content');
        $isShow       = !is_null($request->get('is_show')) ? $request->get('is_show') : 0;
        $classifyId   = !is_null($request->get('classify_id')) ? $request->get('classify_id') : 0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';
        $id           = !is_null($request->get('id')) ? $request->get('id') : 0;

        if($type == 'join'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllHumanRes = HumanResources::whereId($id)->first();

            $isHumanRes = HumanResources::whereClassifyId($classifyId)->first();

            if($newAllHumanRes && ($isHumanRes && $newAllHumanRes->id != $isHumanRes->id))
                return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);

            if(!$newAllHumanRes){
                $newAllHumanRes = new HumanResources();

                if($isHumanRes)
                    return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);
            }

            if(!is_null($content))$newAllHumanRes->content             = $content;
            $newAllHumanRes->is_show                                   = $isShow;
            $newAllHumanRes->classify_id                               = $classifyId;
            $newAllHumanRes->save();
        }elseif($type == 'en_join'){
            if($classifyId == '' || $classifyId == 0)
                return response()->json(['level' => 'error','message' => '请选择分类']);

            $newAllHumanRes = EnHumanResources::whereId($id)->first();

            $isHumanRes = EnHumanResources::whereClassifyId($classifyId)->first();

            if($newAllHumanRes && ($isHumanRes && $newAllHumanRes->id != $isHumanRes->id))
                return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);

            if(!$newAllHumanRes){
                $newAllHumanRes = new EnHumanResources();

                if($isHumanRes)
                    return response()->json(['level' => 'error','message' => '该分类下已有内容，请选择其他分类']);
            }

            if(!is_null($content))$newAllHumanRes->content             = $content;
            $newAllHumanRes->is_show                                   = $isShow;
            $newAllHumanRes->classify_id                               = $classifyId;
            $newAllHumanRes->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }


        return response()->json(["level"=>"success","message"=>"添加或修改成功","data"=>$newAllHumanRes]);
    }

    public function delHumanRes(Request $request){
        if(Auth::guard("admin")->guest())
            return response()->json(['level'=>'error','message' => '请先登录！']);

        $id = !is_null($request->get('id'))?$request->get('id'):0;
        $type         = !is_null($request->get('type')) ? $request->get('type') : '';

        if($type == 'join'){
            $thatHumanRes = HumanResources::whereId($id)->first();
        }elseif($type == 'en_join'){
            $thatHumanRes = EnHumanResources::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatHumanRes)
            return response()->json(["level"=>"error","message"=>"该信息不存在！"]);

        $thatHumanRes->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $thatHumanRes]);
    }

    public function setEnWebSiteInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $title       = $request->get('title');
        $url         = $request->get('url');
        $footerInfo  = $request->get('footer_info');
        $logoId      = !is_null($request->get('logo_id')) ? $request->get('logo_id') : 0;

        if(!is_null($title)){
            $thisTitle = EnSysInfo::whereType('title')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'title';
            }

            $thisTitle->content   = $title;
            $thisTitle->save();
        }

        if(!is_null($url)){
            $thisTitle = EnSysInfo::whereType('url')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'url';
            }

            $thisTitle->content   = $url;
            $thisTitle->save();
        }

        if(!is_null($footerInfo)){
            $thisTitle = EnSysInfo::whereType('footer')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'footer';
            }

            $thisTitle->content   = $footerInfo;
            $thisTitle->save();
        }

        if($logoId != 0){
            $thisTitle = EnSysInfo::whereType('logo')->first();
            if(!$thisTitle){
                $thisTitle = new EnSysInfo();
                $thisTitle->type  = 'logo';
            }

            $thisTitle->content   = $logoId;
            $thisTitle->save();
        }

        return response()->json(['level' => 'success','message' => '信息设置成功']);
    }

    public function addEnFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level' => 'error','message' => '请先登录！']);

        $pid  = !is_null($request->get('pid')) ? $request->get('pid') : 0;
        $name = $request->get('name');
        $link = $request->get('link');

        $isFooterMenu = EnFooterMenu::whereId($pid)->first();
        if(!$isFooterMenu)
            return response()->json(['level' => 'error','message' => '当前父级栏目不存在']);

        $newFooterMenu = new EnFooterMenu();
        $newFooterMenu->pid                     = $pid;
        if(!is_null($name))$newFooterMenu->name = $name;
        if(!is_null($link))$newFooterMenu->link = $link;
        $newFooterMenu->save();

        return response()->json(['level' => 'success','message' => '新增成功','data' => $newFooterMenu]);
    }

    public function delEnFooterMenu(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");

        /** @var EnFooterMenu $theEnMenu */
        $theEnMenu = EnFooterMenu::whereId($id)->first();
        if(!$theEnMenu)
            return response()->json(["level"=>"error","message"=>"当前栏目不存在"]);

        $theEnMenu->delete();

        return response()->json(["level"=>"success","message"=>"信息设置成功"]);

    }

    public function setApplicationField(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id             = !is_null($request->get('id')) ? $request->get('id') : 0;
        $name           = $request->get('name');
        $desc           = $request->get('desc');
        $imgId          = !is_null($request->get('img_id')) ? $request->get('img_id') : 0;
        $thumbImgId     = !is_null($request->get('thumb_img_id')) ? $request->get('thumb_img_id') : 0;
        $content        = $request->get('content');
        $type           = $request->get('type');

        if(!is_null($type) && $type == 'application'){
            $newApplicationField = ApplicationField::whereId($id)->first();
            if(!$newApplicationField){
                $isApplicationField = ApplicationField::all();
                if(count($isApplicationField) >= 3)
                    return response()->json(['level' => 'error','message' => '只能新增三条']);

                $newApplicationField = new ApplicationField();
            }


            $newApplicationField->img_id                           = $imgId;
            $newApplicationField->thumb_img_id                     = $thumbImgId;
            if(!is_null($name))$newApplicationField->name          = $name;
            if(!is_null($desc))$newApplicationField->desc          = $desc;
            if(!is_null($content))$newApplicationField->content    = $content;
            $newApplicationField->save();
        }elseif(!is_null($type) && $type == 'en_application'){
            $newApplicationField = EnApplicationField::whereId($id)->first();
            if(!$newApplicationField){
                $isApplicationField = EnApplicationField::all();
                if(count($isApplicationField) >= 3)
                    return response()->json(['level' => 'error','message' => '只能新增三条']);

                $newApplicationField = new EnApplicationField();
            }


            $newApplicationField->img_id                           = $imgId;
            $newApplicationField->thumb_img_id                     = $thumbImgId;
            if(!is_null($name))$newApplicationField->name          = $name;
            if(!is_null($desc))$newApplicationField->desc          = $desc;
            if(!is_null($content))$newApplicationField->content    = $content;
            $newApplicationField->save();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $newApplicationField]);
    }

    public function delApplicationField(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(["level"=>"error","message"=>"请先登录！"]);

        $id = $request->get("id");
        $type = $request->get('type');

        if(!is_null($type) && $type == 'application') {
            /** @var ApplicationField $thatApplicationField */
            $thatApplicationField = ApplicationField::whereId($id)->first();
        }elseif(!is_null($type) && $type == 'en_application'){
            /** @var EnApplicationField $thatApplicationField */
            $thatApplicationField = EnApplicationField::whereId($id)->first();
        }else{
            return response()->json(['level' => 'error','message' => '系统错误']);
        }

        if(!$thatApplicationField)
            return response()->json(["level"=>"error","message"=>"当前信息不存在"]);

        $thatApplicationField->delete();

        return response()->json(["level"=>"success","message"=>"删除成功",'data' => $thatApplicationField]);
    }
}